package com.example.emergencyattempt01.fragement.user_fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.NetWorkUtils;
import com.example.emergencyattempt01.activity.utils.Pack_DePackUtil;
import com.example.emergencyattempt01.activity.utils.ServerMessage;
import com.example.emergencyattempt01.adapter.St_CheckingAdapter;
import com.example.emergencyattempt01.user.Student;
import com.example.emergencyattempt01.user.User;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Student_Checking_Fragment extends Fragment {

    ListView st_checkingListView;
    List<Map<String,Object>> st_checkingList=new ArrayList<>();
    St_CheckingAdapter checkingAdapter;


    Handler handlerForStChecking=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case LOAD_CHECKING_SUCCESS:
                    Toast.makeText(getActivity(),"当前有"+st_checkingList.size()+"条申请待处理",Toast.LENGTH_LONG).show();
                    checkingAdapter.notifyDataSetChanged();
                    break;
                case LOAD_CHECKING_NOT_FOUND:
                    Toast.makeText(getActivity(),"当前没有任何请求",Toast.LENGTH_SHORT).show();
                    checkingAdapter.notifyDataSetChanged();
                    break;
                case LOAD_CHECKING_FAIL:
                    Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    final int LOAD_CHECKING_SUCCESS=0x025;
    final int LOAD_CHECKING_NOT_FOUND=0x026;
    final int LOAD_CHECKING_FAIL=0x027;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.student_checking_fragment_layout,null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initUser();
        initPage();
        setLoad();
    }

    private void setLoad() {
        new Thread()
        {
            @Override
            public void run() {
                try
                {
                    JSONObject object=new JSONObject();
                    object.put("uid",Student.getInstance().getUid());
                    String toServer=String.valueOf(object);
                    ServerMessage serverMessage=NetWorkUtils.getInstance().create(toServer,"/find_my_requesting");
                    int code=serverMessage.getRespondCode();
                    if(code==200)
                    {
                        String fromServer=(String)serverMessage.getFromServer();
                        if(Pack_DePackUtil.dePackBorrowAhead(fromServer,st_checkingList))
                        {
                            Message message=new Message();
                            message.what=LOAD_CHECKING_SUCCESS;
                            handlerForStChecking.sendMessage(message);
                        }
                        else {
                            Message message=new Message();
                            message.what=LOAD_CHECKING_NOT_FOUND;
                            handlerForStChecking.sendMessage(message);
                        }
                    }
                    else {
                        Message message=new Message();
                        message.what=LOAD_CHECKING_FAIL;
                        handlerForStChecking.sendMessage(message);
                    }

                }catch (JSONException e){e.printStackTrace();}
                catch (IOException e){e.printStackTrace();
                    Message message=new Message();
                    message.what=LOAD_CHECKING_FAIL;
                    handlerForStChecking.sendMessage(message);}
            }
        }.start();
    }

    private void initPage() {
        st_checkingListView=getActivity().findViewById(R.id.st_checking_listView);
        checkingAdapter=new St_CheckingAdapter(getActivity(),st_checkingList,Student.getInstance());
        st_checkingListView.setAdapter(checkingAdapter);
    }

    private void initUser() {
        User student= Student.getInstance();
        User.initUser(getActivity(),student);
    }
}
